﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Note_Final_new
{
        public partial class BookControl : UserControl
    {
        private string bookTitle;
        private Image coverImage;
        private Color coverColor = Color.FromArgb(240, 220, 180);
        private int pageCount;
        private DateTime createdTime;
        private bool isSelected;
        
        public event EventHandler DoubleClick;
        public event MouseEventHandler MouseRightClick;
        public BookControl()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.DoubleBuffer, true);
            this.Size = new Size(180, 250);
            this.Cursor = Cursors.Hand;

        }
        public string Title
        {
            get { return bookTitle; }
            set { bookTitle = value; Invalidate(); }
        }

        public Image CoverImage
        {
            get { return coverImage; }
            set
            {
                coverImage = value;
                // 图片更改时重置封面颜色为中性色，避免干扰
                if (value != null)
                    coverColor = Color.FromArgb(240, 220, 180);
                Invalidate();
            }
        }

        public Color CoverColor
        {
            get { return coverColor; }
            set { coverColor = value; Invalidate(); }
        }

        public int PageCount
        {
            get { return pageCount; }
            set { pageCount = value; Invalidate(); }
        }

        public DateTime CreatedTime
        {
            get { return createdTime; }
            set { createdTime = value; Invalidate(); }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; Invalidate(); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 绘制书本轮廓
            var bookRect = new Rectangle(0, 0, this.Width - 10, this.Height - 10);
            var spineRect = new Rectangle(this.Width - 10, 5, 10, this.Height - 20);

            // 绘制封面
            if (coverImage != null)
            {
                var imageRect = new Rectangle(0, 0, bookRect.Width, bookRect.Height);

                // 使用高质量缩放
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(coverImage, imageRect);

                // 在图片上添加半透明遮罩，增加文字可读性
                using (var overlayBrush = new SolidBrush(Color.FromArgb(80, 0, 0, 0)))
                {
                    g.FillRectangle(overlayBrush, 0, bookRect.Height - 80, bookRect.Width, 80);
                }
            }
            else
            {
                using (var coverBrush = new SolidBrush(coverColor))
                {
                    g.FillRectangle(coverBrush, bookRect);
                }
            }

            //绘制书本书脊
            using (var spineBrush = new SolidBrush(Color.FromArgb(
              coverColor.A,
              Math.Max(0, coverColor.R - 51),  // 约20%的暗度
              Math.Max(0, coverColor.G - 51),
              Math.Max(0, coverColor.B - 51)
             )))
           

            // 绘制书本边缘
            using (var borderPen = new Pen(Color.FromArgb(100, 0, 0, 0), 2))
            {
                g.DrawRectangle(borderPen, bookRect);
                g.DrawRectangle(borderPen, spineRect);
            }

            // 绘制书页效果
            for (int i = 0; i < 5; i++)
            {
                using (var pagePen = new Pen(Color.FromArgb(30 - i * 5, 0, 0, 0)))
                {
                    g.DrawLine(pagePen, 5 + i, 5 + i, bookRect.Width - 5 - i, 5 + i);
                    g.DrawLine(pagePen, 5 + i, bookRect.Height - 5 - i, bookRect.Width - 5 - i, bookRect.Height - 5 - i);
                    g.DrawLine(pagePen, 5 + i, 5 + i, 5 + i, bookRect.Height - 5 - i);
                }
            }

            /*// 如果有封面图片，绘制图片（太冗余）
            if (coverImage != null)
            {
                var imageRect = new Rectangle(10, 10, bookRect.Width - 20, bookRect.Height - 100);
                g.DrawImage(coverImage, imageRect);
            }*/

            // 绘制标题
            using (var titleFont = new Font("Microsoft YaHei", 10, FontStyle.Bold))
            {
                var titleRect = new Rectangle(10, bookRect.Height - 80, bookRect.Width - 20, 60);
                var format = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center,
                    Trimming = StringTrimming.EllipsisWord,
                    FormatFlags = StringFormatFlags.LineLimit
                };

                using (var titleBrush = new SolidBrush(Color.FromArgb(200, 0, 0, 0)))
                {
                    g.DrawString(bookTitle, titleFont, titleBrush, titleRect, format);
                }
            }

            // 绘制底部信息
            using (var infoFont = new Font("Microsoft YaHei", 8))
            {
                var infoText = $"笔记: {pageCount} | {createdTime:yyyy-MM-dd}";
                var infoSize = g.MeasureString(infoText, infoFont);

                using (var infoBrush = new SolidBrush(Color.FromArgb(150, 0, 0, 0)))
                {
                    g.DrawString(infoText, infoFont, infoBrush,
                        bookRect.Width - infoSize.Width - 5,
                        bookRect.Height - infoSize.Height - 5);
                }
            }

            // 如果被选中，绘制选中效果
            if (isSelected)
            {
                using (var selectPen = new Pen(Color.FromArgb(255, 51, 153, 255), 2))
                {
                    g.DrawRectangle(selectPen, 0, 0, this.Width - 1, this.Height - 1);
                }
            }
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);
            DoubleClick?.Invoke(this, e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Right)
            {
                MouseRightClick?.Invoke(this, e);
            }
        }

    }
}
